package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"log"
	"monitor/monitorcon"
	"strconv"
)

//MainController ...  
type MainController struct {
	beego.Controller
}

//StackController ...
type StackController struct {
	beego.Controller
}

//PortController ...
type PortController struct {
	beego.Controller
}

//InterfaceController ...
type InterfaceController struct {
	beego.Controller
}

//CPUController ...
type CPUController struct {
	beego.Controller
}

//MemoryController ...
type MemoryController struct {
	beego.Controller
}

//MacController ...
type MacController struct {
	beego.Controller
}

//RegisterController  登记设备控制器
type RegisterController struct {
	beego.Controller
}

//UnRegisterController  注销设备控制器
type UnRegisterController struct {
	beego.Controller
}

//OperateController  打开或关闭设备采集控制器
type OperateController struct {
	beego.Controller
}


//订阅控制器开始

//StackSubscribeController ...
type StackSubscribeController struct {
	beego.Controller
}

//PortSubscribeController ...
type PortSubscribeController struct {
	beego.Controller
}

//InterfaceSubscribeController ...
type InterfaceSubscribeController struct {
	beego.Controller
}

//CPUSubscribeController ...
type CPUSubscribeController struct {
	beego.Controller
}

//MemorySubscribeController ...
type MemorySubscribeController struct {
	beego.Controller
}

//MacSubscribeController ...
type MacSubscribeController struct {
	beego.Controller
}

//订阅控制器结束

//北向控制类开始

//LogicSwtichController ...
type LogicSwtichController struct {
	beego.Controller
}

//LogicMacController ...
type LogicMacController struct {
	beego.Controller
}

//MacUUIDController ...
type MacUUIDController struct {
	beego.Controller
}

//LogicPortController ...
type LogicPortController struct {
	beego.Controller
}

//PortUUIDController ...
type PortUUIDController struct {
	beego.Controller
}

//Get MainController for beego main page
func (c *MainController) Get() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"
}

//Get 得到stack info
func (c *StackController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetStackinfofromredis(rs, hostid)
	log.Println(resp)
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	return
}

//Get  subscribe stack info
func (c *StackSubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")

	rs := monitorcon.GetrsManager()
	monitorcon.Subscribefieldinfofromredis(rs, hostid, "stack", "") //最后一项item: position,id,iframe value
	log.Println("subscribe stack suc")
	return
}

//Get 得到port info
func (c *PortController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	log.Println("port position is ", position)

	if position == "" {
		log.Println("port position is nil")
	}
	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetPortfromredis(rs, hostid, position)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get 订阅port info
func (c *PortSubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	log.Println("port position is ", position)

	rs := monitorcon.GetrsManager()

	monitorcon.Subscribefieldinfofromredis(rs, hostid, "port", position)

	log.Println("subscribe port suc")
	return
}

//Get 得到interface info
func (c *InterfaceController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var ifname = c.Ctx.Input.Param(":ifname")
	if ifname == "" {
		log.Println("interface ifname is nil")
	}

	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetInterfaceInfofromredis(rs, hostid, ifname)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get 订阅interface info
func (c *InterfaceSubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var ifname = c.Ctx.Input.Param(":ifname")
	log.Println("interface ifname is ", ifname)

	rs := monitorcon.GetrsManager()

	monitorcon.Subscribefieldinfofromredis(rs, hostid, "interface", ifname)

	log.Println("subscribe interface suc")
	return
}

//Get 得到cpu info
func (c *CPUController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	if position == "" {
		log.Println("cpu position is nil")
	}
	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetCPUInfofromredis(rs, hostid, position)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	return
}

//Get 订阅cpu info
func (c *CPUSubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	log.Println("Cpu position is ", position)

	rs := monitorcon.GetrsManager()

	monitorcon.Subscribefieldinfofromredis(rs, hostid, "cpu", position)

	log.Println("subscribe cpu suc")
	return
}

//Get 得到memory info
func (c *MemoryController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	if position == "" {
		log.Println("memory position is nil")
	}
	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetMemoryInfofromredis(rs, hostid, position)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get 订阅memory info
func (c *MemorySubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var position = c.Ctx.Input.Param(":position")
	log.Println("Memory position is ", position)

	rs := monitorcon.GetrsManager()

	monitorcon.Subscribefieldinfofromredis(rs, hostid, "memory", position)

	log.Println("subscribe memory suc")
	return
}

//Get 得到mac info
func (c *MacController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var id = c.Ctx.Input.Param(":id")
	if id == "" {
		log.Println("mac id is nil")
	}
	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetMacInfofromredis(rs, hostid, id)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get 订阅mac info
func (c *MacSubscribeController) Get() {

	var hostid = c.Ctx.Input.Param(":hostid")
	var id = c.Ctx.Input.Param(":id")
	log.Println("mac id is ", id)

	rs := monitorcon.GetrsManager()

	monitorcon.Subscribefieldinfofromredis(rs, hostid, "mac", id)

	log.Println("subscribe mac suc")
	return
}

//Get 设备登记
func (c *RegisterController) Get() {

	//var hostid = c.Ctx.Input.Param(":hostid")
	hostid := c.Input().Get("hostid")
	log.Println("hostid=", hostid)

	if hostid == "" {
		return
	}
	
	monitorcon.RegisterDevice(hostid)
	return
}

//Get 设备删除
func (c *UnRegisterController) Get() {

	//var hostid = c.Ctx.Input.Param(":hostid")
	hostid := c.Input().Get("hostid")
	log.Println("hostid=", hostid)

	if hostid == "" {
		return
	}
	
	monitorcon.UnRegisterDevice(hostid)
	return
}

//Get 打开或关闭设备
func (c *OperateController) Get() {

	hostid := c.Input().Get("hostid") //c.Ctx.Input.Param(":hostid")
	optype := c.Input().Get("type")
	log.Println("type=", optype)
	log.Println("hostid=", hostid)
	if optype == "" {
		return
	}

	if hostid == "" {
		return
	}

	monitorcon.OperateDevice(hostid, optype)

	return
}


//Get LogicSwtichController 北向设备信息集成 (1.1)
func (c *LogicSwtichController) Get() {
	var switchuuid = c.Ctx.Input.Param(":id")
	log.Println("begin to handle ", switchuuid)
	if switchuuid == "" {
		return
	}

	rs := monitorcon.GetrsManager()
	var resp = monitorcon.GetSwitchInfofromredis(rs, switchuuid)
	
	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(resp), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get LogicMacController 北向mac信息 (1.4)
func (c *LogicMacController) Get() {
	var switchuuid = c.Ctx.Input.Param(":id")
	log.Println("begin to handle ", switchuuid)
	if switchuuid == "" {
		return
	}

	skip := c.Input().Get("skip") //c.Ctx.Input.Param(":hostid")
	top := c.Input().Get("top")
	count := c.Input().Get("count")

	filter := c.Input().Get("filter")
	value := c.Input().Get("value")

	map1 := make(map[string]interface{})
	map1["@odata.id"] = ""              //default
	map1["Members@odata.nextLink"] = "" //default

	rs := monitorcon.GetrsManager()
	list := monitorcon.GetMacmapInfofromredis(rs, switchuuid)
	var listlen = len(list)
	var flag bool
	flag = true

	if count != "" { //for count
		map2 := make(map[string]interface{})
		map2["count"] = len(list)
		b, err := json.Marshal(map2)

		if err != nil {
			c.Data["json"] = ""

		}
		
		respmap := make(map[string]interface{})
		err = json.Unmarshal([]byte(b), &respmap)
		if (err!=nil){
		   log.Println("change to map error")
		   return 
		}
		c.Data["json"] = respmap
		c.ServeJSON()
		
		return

	}

	if skip != "" {
		skiplen, err := strconv.Atoi(skip)
		if err != nil {
			//log.Println(err)
			return
		}
		if skiplen < len(list) {
			//map1["Members@odata.count"]=len(list)-skiplen
			listlen = len(list) - skiplen
			list = list[skiplen:]

		} else {
			flag = false

		}

		if top != "" {
			toplen, err := strconv.Atoi(top)
			if err != nil {
				//log.Println(err)
				return
			}
			if toplen <= len(list) {
				list = list[0:toplen]
				listlen = toplen
				//map1["Members@odata.count"]=len(list)-skiplen
				//map1["Members"]=list[skiplen:]
			} else {
				flag = false
				//map1["Members@odata.count"]=0
				//map1["Members"]=""
			}

		}

	}
	var llen int
	if filter != "" && flag == true {
		list, llen = monitorcon.Searchvaluefromlist(list, filter, value)
		listlen = llen
		list = list[0:llen]
	}

	if flag == true {
		map1["Members@odata.count"] = listlen
		map1["Members"] = list
		resp, err := json.Marshal(map1)

		if err == nil {
			
			//c.Data["json"] = string(resp)
			
			respmap := make(map[string]interface{})
			var err = json.Unmarshal([]byte(resp), &respmap)
			if (err!=nil){
			   log.Println("change to map error")
			   return 
			}
			c.Data["json"] = respmap
			
			

		}
	} else {

		c.Data["json"] = string("")
	}

	c.ServeJSON()

	return

}

//Get MacUUIDController 北向mac uuid info (1.5)
func (c *MacUUIDController) Get() {

	var hostid = c.Ctx.Input.Param(":id")
	var uuid = c.Ctx.Input.Param(":uuid")
	if uuid == "" {
		log.Println("mac uuid is nil")
	}
	rs := monitorcon.GetrsManager()

	resp, _ := rs.GetDevicefield(hostid, "mac")

	subuuidinfo := monitorcon.Searchvalue(resp, uuid)


	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(subuuidinfo), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
	c.ServeJSON()
	
	return
}

//Get LogicPortController 北向port 信息 (1.2)
func (c *LogicPortController) Get() {
	var switchuuid = c.Ctx.Input.Param(":id")
	log.Println("begin to handle ", switchuuid)
	if switchuuid == "" {
		return
	}

	skip := c.Input().Get("skip") //c.Ctx.Input.Param(":hostid")
	top := c.Input().Get("top")
	count := c.Input().Get("count")

	filter := c.Input().Get("filter")
	value := c.Input().Get("value")

	map1 := make(map[string]interface{})
	map1["@odata.id"] = ""              //default
	map1["Members@odata.nextLink"] = "" //default

	rs := monitorcon.GetrsManager()
	list := monitorcon.GetPortmapInfofromredis(rs, switchuuid)
	log.Println(list)
	var listlen = len(list)
	log.Println(listlen)
	var flag bool
	flag = true

	if count != "" { //for count
		map2 := make(map[string]interface{})
		map2["count"] = len(list)
		b, err := json.Marshal(map2)

		if err != nil {
			c.Data["json"] = ""

		}
		//c.Data["json"] = string(b)
		
		respmap := make(map[string]interface{})
		err = json.Unmarshal([]byte(b), &respmap)
		if (err!=nil){
		   log.Println("change to map error")
		   return 
		}
		c.Data["json"] = respmap
		c.ServeJSON()
		return

	}

	if skip != "" {
		skiplen, err := strconv.Atoi(skip)
		if err != nil {
			//log.Println(err)
			return
		}
		if skiplen < len(list) {
			//map1["Members@odata.count"]=len(list)-skiplen
			listlen = len(list) - skiplen
			list = list[skiplen:]
			log.Println("len=", listlen)
			log.Println(list)
		} else {
			flag = false

		}

		if top != "" {
			toplen, err := strconv.Atoi(top)
			if err != nil {
				//log.Println(err)
				return
			}
			if toplen <= len(list) {
				list = list[0:toplen]
				listlen = toplen
				//map1["Members@odata.count"]=len(list)-skiplen
				//map1["Members"]=list[skiplen:]
			} else {
				flag = false
				//map1["Members@odata.count"]=0
				//map1["Members"]=""
			}

		}

	}
	var llen int
	if filter != "" && flag == true {
		list, llen = monitorcon.Searchvaluefromlist(list, filter, value)
		listlen = llen
		list = list[0:llen]
	}

	if flag == true {
		map1["Members@odata.count"] = listlen
		map1["Members"] = list
		//log.Println(map1)
		resp, err := json.Marshal(map1)
		//log.Println(resp)
		if err == nil {
			//c.Data["json"] = string(resp)
			respmap := make(map[string]interface{})
			var err = json.Unmarshal([]byte(resp), &respmap)
			if (err!=nil){
			   log.Println("change to map error")
			   return 
			}
			c.Data["json"] = respmap
			
			
		}
	} else {

		c.Data["json"] = string("")
	}

	c.ServeJSON()

	return

}

//Get 北向port UUID信息 (1.3)
func (c *PortUUIDController) Get() {

	var hostid = c.Ctx.Input.Param(":id")
	var uuid = c.Ctx.Input.Param(":uuid")
	if uuid == "" {
		log.Println("port uuid is nil")
	}
	rs := monitorcon.GetrsManager()

	resp, _ := rs.GetDevicefield(hostid, "port")
	log.Println(resp)

	subuuidinfo := monitorcon.Searchvalue(resp, uuid)

	respmap := make(map[string]interface{})
	var err = json.Unmarshal([]byte(subuuidinfo), &respmap)
	if (err!=nil){
	   log.Println("change to map error")
	   return 
	}
	c.Data["json"] = respmap
		
	return
}
